﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;
using ALiteTeam.PackageTracer.Core;
using Microsoft.Phone.Shell;
using GalaSoft.MvvmLight.Messaging;
using ALiteTeam.PackageTracer.ViewModel;
using Microsoft.Phone.Tasks;
using System.Text;

namespace ALiteTeam.PackageTracer.View.View
{
    public partial class Details : PhoneApplicationPage
    {
        private ApplicationBarIconButton _AppBarBtnRefresh;
        private ApplicationBarIconButton _AppBarBtnCall;
        private ApplicationBarIconButton _AppBarBtnMsg;

        public Details()
        {
            InitializeComponent();

            this._AppBarBtnRefresh = new ApplicationBarIconButton(App.Current.GetIconUri(AppBarIcons.Refresh));
            this._AppBarBtnRefresh.Text = "刷新";
            this._AppBarBtnRefresh.Click += new EventHandler(_AppBarBtnRefresh_Click);
            
            this._AppBarBtnCall = new ApplicationBarIconButton(App.Current.GetIconUri(AppBarIcons.Call));
            this._AppBarBtnCall.Text = "联系快递";
            this._AppBarBtnCall.Click += new EventHandler(_AppBarBtnCall_Click);

            this._AppBarBtnMsg = new ApplicationBarIconButton(App.Current.GetIconUri(AppBarIcons.Message));
            this._AppBarBtnMsg.Text = "短信转发";
            this._AppBarBtnMsg.Click += new EventHandler(_AppBarBtnMsg_Click);

            ApplicationBar.Buttons.Add(_AppBarBtnMsg);
            ApplicationBar.Buttons.Add(_AppBarBtnCall);
            ApplicationBar.Buttons.Add(_AppBarBtnRefresh);
        }

        protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e)
        {
            if (NavigationContext.QueryString.Keys.Contains("OrderID") == true)
            {
                var orderID = NavigationContext.QueryString["OrderID"];

                Settings.Configuration.UpdateOrder.Remove(new Guid(orderID));
                TileHandlerForAgent.ResetDefaultTile(Settings.Configuration.UpdateOrder.Count);

                // 刷新页面数据
                Messenger.Default.Send<Guid>(new Guid(orderID), DetailsViewModel.INITIALIZE);

                // 刷新Details
                Messenger.Default.Send<NotificationMessage>(new NotificationMessage(this, this.performanceProgressBar, string.Empty), DetailsViewModel.REFRESH);
            }
            base.OnNavigatedTo(e);
        }

        void _AppBarBtnMsg_Click(object sender, EventArgs e)
        {
            var viewModel = this.DataContext as DetailsViewModel;

            PhoneNumberChooserTask task = new PhoneNumberChooserTask();

            task.Completed += (s, evt) =>
            {
                if (evt.Error == null && evt.TaskResult == TaskResult.OK)
                {
                    SmsComposeTask taskSms = new SmsComposeTask();

                    taskSms.To = evt.PhoneNumber;

                    StringBuilder content = new StringBuilder();

                        content.Append(viewModel.DisplayName);
                        content.Append(viewModel.OrderNo);
                        content.Append(": ");
                        foreach (var item in viewModel.Details)
                        {
                            content.Append(item.Time);
                            content.Append("=>");
                            content.Append(item.Context);
                            content.Append(';');
                        }
                    taskSms.Body = content.ToString();

                    taskSms.Show();
                }
            };

            task.Show();
        }

        void _AppBarBtnCall_Click(object sender, EventArgs e)
        {
            var viewModel = this.DataContext as DetailsViewModel;

            PhoneCallTask task = new PhoneCallTask();
            task.PhoneNumber = viewModel.Phone.Replace("-", string.Empty);
            task.DisplayName = viewModel.DisplayName;
            task.Show();
        }

        void _AppBarBtnRefresh_Click(object sender, EventArgs e)
        {
            // TODO: Toolkit bug
            //Messenger.Default.Send<NotificationMessage>(new NotificationMessage(this, string.Empty), DetailsViewModel.REFRESH);
            Messenger.Default.Send<NotificationMessage>(new NotificationMessage(this, this.performanceProgressBar, string.Empty), DetailsViewModel.REFRESH);
        }
    }
}